
import React from 'react'

import Odoo from '../odoo'

export const actions = {
  REFRESH_IMAGES: 'REFRESH_IMAGES',
  LOADMORE_IMAGES: 'LOADMORE_IMAGES'
}

export const statuses = {
  CONNECT_SUCCESS: 'CONNECT_SUCCESS',
  CONNECT_ERROR: 'CONNECT_ERROR',
  CONNECTING: 'CONNECTING',
  SEARCHING_IMAGE: 'SEARCHING_IMAGE',
  SEARCH_IMAGE_SUCCESS: 'SEARCH_IMAGE_SUCCESS',
  SEARCH_IMAGE_ERROR: 'SEARCH_IMAGE_ERROR'
}

export const types = {
  ODOO_CONNECT: 'ODOO_CONNECT',
  ODOO_SEARCH_IMAGE: 'ODOO_SEARCH_IMAGE',
  REFRESH_IMAGES:'REFRESH_IMAGES',
};

export const actionCreators = {
  connect: () => async (dispatch, getState) => {
    dispatch({ type: types.ODOO_CONNECT, status: statuses.CONNECTING })

    config = {
      host: '192.168.31.107',
      port: 8069,
      database: 'example',
      username: 'admin',
      password: '1988227227'
    }
    const odoo = new Odoo(config);

    try {
      await odoo.connect();
      dispatch({
        type: types.ODOO_CONNECT,
        status: statuses.CONNECT_SUCCESS,
        odoo
      });
      // 登录成功后进行一次抓取
      actionCreators.getImages()(dispatch, getState)
    } catch (e) {
      dispatch({
        type: types.ODOO_CONNECT,
        status: statuses.CONNECT_ERROR,
        error: e
      });
    }
  },
  // 从服务器获取图片
  getImages: ( action='' ) => async (dispatch, getState) => {
    dispatch({ type: types.ODOO_SEARCH_IMAGE, status: statuses.SEARCHING_IMAGE})
    const odoo  = getState().odoo
    let params = {
      fields: [
        'username', 'img_src', 'desc',
        'ctime', 'height', 'width', 'resized_url'
      ],
      // 获取12点之后的数据 ['ctime', '>', '2016-12-21 12:00:00']
      domain: [],
      // 字段加desc可以改变排列的顺序
      order: 'ctime desc',
      // 从排序的列表偏移10个后开始取值，不包含第10个
      limit: 20
    }
    if (action === actions.REFRESH_IMAGES && odoo.latestCtime) {
      // 已经抓取过一次
      params.domain.push(['ctime', '>', odoo.latestCtime])
      dispatch({type: types.REFRESH_IMAGES, loading: true})
    }

    if (action === actions.LOADMORE_IMAGES && odoo.earliestTime) {
      // 每次加载10条
      params.limit = 10
      params.domain.push(['ctime', '<', odoo.earliestTime])
    }

    try {
      const response = await odoo.odoo.search_read('spider.pic', params)
      const rspDatas = await response.json()
      let imageDatas = rspDatas.result
      // 根据返回的最新数据对odoo.imageDatas数据进行更新
      // 大于某个时间获得的数据不一定是20个
      if (action === actions.REFRESH_IMAGES) {
        // 原来的数据放在新数组的尾部
        imageDatas = [...imageDatas, ...odoo.imageDatas]
      }

      if (action === actions.LOADMORE_IMAGES) {
        imageDatas = [...odoo.imageDatas, ...imageDatas]
      }

      const len = imageDatas.length
      const latestCtime = len ? imageDatas[0].ctime : ''
      const earliestTime = len ? imageDatas[len-1].ctime : ''
      dispatch({
        type: types.ODOO_SEARCH_IMAGE,
        status: statuses.SEARCH_IMAGE_SUCCESS,
        imageDatas,
        latestCtime,
        earliestTime
      })
      // 结束ListView的刷新状态
      if (action === actions.REFRESH_IMAGES) {
        dispatch({type: types.REFRESH_IMAGES, loading: false})
      }
    } catch (e) {
      dispatch({
        type: types.ODOO_SEARCH_IMAGE,
        status: statuses.SEARCH_IMAGE_ERROR,
        error:e
      })
    }
  },
  // 从服务器获取段子
  getTexts: ( action='' ) => async (dispatch, getState) => {

  },
  //
};

// const initialState = {
//   odoo: null
// };

const initialState = {
  imageDatas:[],
  latestCtime:'',
  earliestTime:'',
  loading: false
}

export const reducer = (state = initialState, action) => {
  const {
    type, status, odoo, error, loading,
    imageDatas, latestCtime, earliestTime
  } = action;
  switch (type) {
    case types.ODOO_CONNECT: {
      if (status === statuses.CONNECTING) {
        return { ...state, status }
      }
      if (status === statuses.CONNECT_SUCCESS) {
        return { ...state, odoo, status }
      }
      if (status === statuses.CONNECT_ERROR) {
        return { ...state, status, error }
      }
    }
    case types.ODOO_SEARCH_IMAGE: {
      if(status === statuses.SEARCHING_IMAGE) {
        return { ...state, status }
      }
      if( status === statuses.SEARCH_IMAGE_SUCCESS ) {
        return {
          ...state,
          status,
          imageDatas,
          latestCtime,
          earliestTime
         }
      }
      if ( status === statuses.SEARCH_IMAGE_ERROR ) {
        return { ...state, status, error }
      }
    }
    case types.REFRESH_IMAGES: {
      return { ...state, loading }
    }
    default: {
      return state
    }
  }
};
